package com.mju.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.mju.exception.ServiceException;
import com.mju.listener.ExcelTeacherListener;
import com.mju.model.Teacher;
import com.mju.model.dto.DurationDTO;
import com.mju.model.dto.SelectTeacherDTO;
import com.mju.service.SelectTeacherService;
import com.mju.service.TeacherService;
import com.mju.utils.RespData;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {

	@Autowired
	private TeacherService teacherService;	
	
	@Autowired
	private SelectTeacherService selectTeacherService;

	@PostMapping("/impExcel")
	public RespData impExcel(MultipartFile file) throws IOException {
		ExcelTeacherListener listener = new ExcelTeacherListener();

		EasyExcel.read(file.getInputStream(), Teacher.class, listener).sheet().doRead();

		List<Teacher> list = listener.getList();
		teacherService.batchInsert(list);
		return RespData.success("成功导入" + list.size() + "条", listener.getErrList());
	}

	@GetMapping("/list")
	public RespData list(Integer pageNo, Integer pageSize) {
		Page<Teacher> page = teacherService.getList(pageNo, pageSize);
		return RespData.data(page);
	}

	@SuppressWarnings("unchecked")
	@GetMapping("/duration")
	public RespData duration() throws ServiceException {
		DurationDTO dto = teacherService.selectDuaration();

		ObjectMapper m = new ObjectMapper();
		Map<String, Object> map = new HashMap<>();
		map = m.convertValue(dto, Map.class);
		return RespData.data(map);
	}

	// 学生选择导师，发送消息,表单方式
	@PostMapping("/choice")
	public RespData choice(String teacher1, String teacher2) {

		Map<String,String> retMap= teacherService.sendStreamMsg(teacher1,teacher2);

		return RespData.data(retMap);
	}

	// 导师选择学生，获取消息,表单方式,轮次可以由管理员设置，就可以自动获取
	@GetMapping("/getChoice")
	public RespData choice(int turnNum) {

		List<Map<String,String>> list= teacherService.receiveMsg(turnNum);

		return RespData.data(list);
	}
	
	// 导师选择学生，消息存入数据库，删除处理过的消息,表单方式
	@PostMapping("/dealChoice")
	public RespData deal(String studentno, String msgId, Long time) {
		selectTeacherService.dealChoice(studentno, msgId, time);
		return RespData.success("选择成功！");
	}
		
	//获取当前教师信息
	@GetMapping("/me")
	public RespData me() {
		Teacher t=teacherService.getTeacherById();
		ObjectMapper m = new ObjectMapper();
		Map<String, Object> map = new HashMap<>();
		map = m.convertValue(t, Map.class);
		return RespData.data(map);
	}
	
	//根据教师获取当前选择的学生
	@GetMapping("/getSelect")
	public RespData getSelect() {
		List<SelectTeacherDTO> list=selectTeacherService.selectList();
		return RespData.data(list);
	}
}
